<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>

        <input value="你好" id="sendValue" />
        <br />
        <button onclick="send()">点击发送</button>
        
        <div id="responseText" style="width: 500px;height: 500px;background-color: bisque;"></div>
    </body>

    <script>
        var socket;

        if (!window.WebSocket) {

            window.WebSocket = window.MozWebSocket;
        }

        if (window.WebSocket) {
            socket = new WebSocket("ws://localhost:9923/wsNetty");

            socket.onmessage = function(event) {

                var ta = document.getElementById('responseText');
                ta.innerText += event.data + "\r\n";
            };

            socket.onopen = function(event) {

                var ta = document.getElementById('responseText');
                ta.innerText  = "打开WebSoket 服务正常，浏览器支持WebSoket!" + "\r\n";

            };

            socket.onclose = function(event) {

                var ta = document.getElementById('responseText');
                ta.innerText = "";
                ta.innerText = "WebSocket 关闭" + "\r\n";
            };
        } else {
            alert("您的浏览器不支持WebSocket协议！");
        }

        function send() {
            if (!window.WebSocket) {
                return;
            }
            
            var dom_sendValue = document.getElementById("sendValue")
            if (socket.readyState == WebSocket.OPEN) {
                socket.send(dom_sendValue.value);
            } else {
                alert("WebSocket 连接没有建立成功！");
            }

        }
    </script>
</html>
